package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.service.AudioService;
import org.example.service.CsvInMysqlService;
import org.example.service.TranslationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据同步控制器
 * 提供同步数据的接口
 */
@RestController
@RequestMapping("/api/sync")
@Slf4j
public class SyncController {

    @Autowired
    private CsvInMysqlService csvInMysqlService;
    
    @Autowired
    private TranslationService translationService;
    
    @Autowired
    private AudioService audioService;

    /**
     * 同步数据接口
     * @return 同步结果
     */
    @GetMapping("/syndata")
    public ResponseEntity<Map<String, Object>> syncData() {
        log.info("开始CSV数据同步操作");
        Map<String, Object> result = new HashMap<>();

        try {
            log.debug("调用csvInMysqlService.importAllCsvFiles()开始导入");
            // 调用服务执行导入
            csvInMysqlService.importAllCsvFiles();

            // 构建成功响应
            result.put("success", true);
            result.put("message", "CSV数据开始导入");
            log.info("CSV数据导入任务已启动");

            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("CSV导入失败: {}", e.getMessage(), e);

            // 构建错误响应
            result.put("success", false);
            result.put("message", "CSV导入失败: " + e.getMessage());

            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 对接百度翻译API接口
     * 
     * @param requestBody 包含要翻译的英文单词的请求体
     * @return 翻译结果
     */
    @PostMapping("/translate")
    public ResponseEntity<Map<String, Object>> translateWord(@RequestBody Map<String, String> requestBody) {
        log.info("接收到翻译请求");
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 获取要翻译的英文单词
            String word = requestBody.get("word");
            
            if (word == null || word.trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "请提供要翻译的英文单词");
                return ResponseEntity.badRequest().body(result);
            }
            
            log.debug("翻译单词: {}", word);
            
            // 调用翻译服务
            String translation = translationService.translateEnToZh(word);
            
            // 构建成功响应
            result.put("success", true);
            result.put("word", word);
            result.put("translation", translation);
            
            log.info("翻译成功: {} -> {}", word, translation);
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("翻译失败: {}", e.getMessage(), e);
            
            // 构建错误响应
            result.put("success", false);
            result.put("message", "翻译失败: " + e.getMessage());
            
            return ResponseEntity.badRequest().body(result);
        }
    }
    
    /**
     * 获取英文单词的发音
     * 
     * @param word 英文单词
     * @param type 发音类型：american(美式)或british(英式)，默认为american
     * @return 发音音频URL
     */
    @GetMapping("/pronunciation")
    public ResponseEntity<Map<String, Object>> getPronunciation(
            @RequestParam String word,
            @RequestParam(defaultValue = "american") String type) {
        
        log.info("接收到获取发音请求，单词: {}, 类型: {}", word, type);
        Map<String, Object> result = new HashMap<>();
        
        try {
            if (word == null || word.trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "请提供要获取发音的英文单词");
                return ResponseEntity.badRequest().body(result);
            }
            
            String pronunciationUrl;
            
            // 根据类型获取相应的发音URL
            if ("british".equalsIgnoreCase(type)) {
                pronunciationUrl = audioService.getBritishPronunciationUrl(word);
                log.debug("获取英式发音URL: {}", pronunciationUrl);
            } else {
                // 默认为美式发音
                pronunciationUrl = audioService.getAmericanPronunciationUrl(word);
                log.debug("获取美式发音URL: {}", pronunciationUrl);
            }
            
            // 构建成功响应
            result.put("success", true);
            result.put("word", word);
            result.put("type", type);
            result.put("url", pronunciationUrl);
            
            log.info("获取发音成功: {}", word);
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("获取发音失败: {}", e.getMessage(), e);
            
            // 构建错误响应
            result.put("success", false);
            result.put("message", "获取发音失败: " + e.getMessage());
            
            return ResponseEntity.badRequest().body(result);
        }
    }
}
